const list = (function () {
    axios.defaults.baseURL = "";
    const loadData = async () => {
        axios.interceptors.request.use((config) => {
            config.headers.token = localStorage.getItem("token");
            return config;
        });
        axios({
            url: "/cart/getCart"
        }).then(function (res) {
            render(res.data.data);
        })

        function render(data) {
            let str = ``
            let je = 0;
            let strhtml = ``;
            let su=0;
            data.forEach(el => {
                su++;
                str += `
                <tr>
                <td>
                    <input class="fxzk" type="checkbox"><img src="./${el.simg}" alt="">
                    <input class="sssi" style="display:none" value="${el.sid}" type="text">
                </td>
                <td>
                    <a href="">${el.sname}</a>
                </td>
                <td style="width: 70px;">
                    ￥<em class="djj">${el.sprice}</em>
                </td>
                <td style="width: 70px;">
                    -
                </td>
                <td class="jssl" style="width: 140px;">
                    <button class="sppj">-</button><input class="ssl" type="text" value="${el.ssum}" name=""
                        id=""><button class="spj">+</button>
                </td>
                <td style="width: 70px;">
                    ￥<em class="xj">${el.stotal}</em>
                </td>
                <td class="cz" style="width: 70px;">
                    <a class="sc" c_id=${el.sid} href="">删除</a>
                </td>
            </tr>
            `
                je += parseInt(el.stotal);
            });
            strhtml = `
            <p><span>应付商品金额:</span> <span class="jq">¥${je}.00</span></p>
            <p><span>跨境商品总金额:</span> <span class="jr">¥0.00</span></p>
            <p><span>普通商品总金额:</span> <span class="jr jjr">¥${je}.00</span></p>
            `;
            $(".tab").html(str);
            $(".je").html(strhtml);
            $(".gwjs").html(`<a href="">购物车<em>${su}</em>件</a>`);
            ss();
        }

        function ss() {
            $(".jssl").on("click", '.sppj', function () {
                if ($(this).next('.ssl').val() == 1) {
                    return;
                }
                $(this).next('.ssl').val($(this).next('.ssl').val() - 1)
                totalROW(this)
            })

            $(".jssl").on("click", '.spj', function () {
                $(this).prev('.ssl').val(Number($(this).prev('.ssl').val()) + 1);
                totalROW(this)
            })
            //删除
            $(".tab").on("click", '.sc', function () {
                let spid = $(this).attr("c_id");
                let data = new URLSearchParams();
                data.append("spid", spid);
                layer.confirm("你确定要删除吗?", {
                    btn: ["是", '否']
                }, (index) => {
                    axios({
                        url: "/cart/deleteCart",
                        method: "post",
                        data
                    }).then((res) => {
                        console.log(res)
                        layer.msg(res.data.msg);
                        layer.close(index);
                        $(this).parents("tr").remove();
                        let jee=0;
                        Array.from($(".xj")).forEach(el=>{
                            jee+=parseInt($(el).text());
                        });
                        $(".jq").html("¥"+jee+".00");
                        $(".jjr").html("¥"+jee+".00");
                    })

                }, function () {

                })
                return false;
            })

            //结算
            $(".qjssp").on("click", '.js2', function () {
               Array.from($(".fxzk")).forEach(el=>{
                    if($(el).is(":checked")){
                        let spid=$(el).parent().find(".sssi").val();
                        let data = new URLSearchParams();
                        data.append("spid", spid);
                            axios({
                                url: "/cart/deleteCart",
                                method: "post",
                                data
                            }).then((res) => {
                                layer.msg(res.data.msg);
                                console.log($(this));
                                $(el).parents("tr").remove();
                                let jee=0;
                                Array.from($(".xj")).forEach(el=>{
                                    jee+=parseInt($(el).text());
                                });
                                $(".jq").html("¥"+jee+".00");
                                $(".jjr").html("¥"+jee+".00");
                            })
                    }
                })
                   
               
                return false;
            })


            function totalROW(obj) {
                let sid=$(obj).parents("tr").find(".sssi").val();
                let num = $(obj).parent().find(".ssl").val();
                let price =$(obj).parents("tr").find(".djj").text();
                $(obj).parents("tr").find(".xj").text(num * price);
                let jee=0;
                Array.from($(".xj")).forEach(el=>{
                    jee+=parseInt($(el).text());
                });
                $(".jq").html("¥"+jee+".00");
                $(".jjr").html("¥"+jee+".00");
                let data = new URLSearchParams();
                data.append("sid", sid);
                data.append("ssum", num);
                axios({
                    url: "/cart/updataCart",
                    method: "post",
                    data
                }).then(function (res) {
                    layer.msg(res.data.msg);
                }) 
            }
        }

    }
    return {
        loadData
    }
})()
$(function () {
    list.loadData()
});